package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.BaseDataLogisticsCompanyMapper;
import com.ruoyi.cderp.domain.BaseDataLogisticsCompany;
import com.ruoyi.cderp.service.IBaseDataLogisticsCompanyService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 物流公司Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-19
 */
@Service
public class BaseDataLogisticsCompanyServiceImpl implements IBaseDataLogisticsCompanyService
{
    @Autowired
    private BaseDataLogisticsCompanyMapper baseDataLogisticsCompanyMapper;

    LoginUser loginUser = null;

    /**
     * 查询物流公司
     *
     * @param id 物流公司主键
     * @return 物流公司
     */
    @Override
    public BaseDataLogisticsCompany selectBaseDataLogisticsCompanyById(String id)
    {
        return baseDataLogisticsCompanyMapper.selectBaseDataLogisticsCompanyById(id);
    }

    /**
     * 查询物流公司列表
     *
     * @param baseDataLogisticsCompany 物流公司
     * @return 物流公司
     */
    @Override
    public List<BaseDataLogisticsCompany> selectBaseDataLogisticsCompanyList(BaseDataLogisticsCompany baseDataLogisticsCompany)
    {
        return baseDataLogisticsCompanyMapper.selectBaseDataLogisticsCompanyList(baseDataLogisticsCompany);
    }

    /**
     * 新增物流公司
     *
     * @param baseDataLogisticsCompany 物流公司
     * @return 结果
     */
    @Override
    public int insertBaseDataLogisticsCompany(BaseDataLogisticsCompany baseDataLogisticsCompany)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        baseDataLogisticsCompany.setId("10" + formattedDate+randomNumber);
        baseDataLogisticsCompany.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataLogisticsCompany.setCreateById(loginUser.getUser().getUserId().toString());
        baseDataLogisticsCompany.setCreateBy(loginUser.getUser().getUserName());
        baseDataLogisticsCompany.setCreateTime(DateUtils.getNowDate());
        return baseDataLogisticsCompanyMapper.insertBaseDataLogisticsCompany(baseDataLogisticsCompany);
    }

    /**
     * 修改物流公司
     *
     * @param baseDataLogisticsCompany 物流公司
     * @return 结果
     */
    @Override
    public int updateBaseDataLogisticsCompany(BaseDataLogisticsCompany baseDataLogisticsCompany)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataLogisticsCompany.setUpdateById(loginUser.getUser().getUserId().toString());
        baseDataLogisticsCompany.setUpdateBy(loginUser.getUser().getUserName());
        baseDataLogisticsCompany.setUpdateTime(DateUtils.getNowDate());
        return baseDataLogisticsCompanyMapper.updateBaseDataLogisticsCompany(baseDataLogisticsCompany);
    }

    /**
     * 批量删除物流公司
     *
     * @param ids 需要删除的物流公司主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataLogisticsCompanyByIds(String[] ids)
    {
        return baseDataLogisticsCompanyMapper.deleteBaseDataLogisticsCompanyByIds(ids);
    }

    /**
     * 删除物流公司信息
     *
     * @param id 物流公司主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataLogisticsCompanyById(String id)
    {
        return baseDataLogisticsCompanyMapper.deleteBaseDataLogisticsCompanyById(id);
    }
}
